
	mui.init({
				pullRefresh: {
					container: '#pullrefresh',
					up: {
						auto:false,
						contentrefresh: '正在加载...',
						callback: pullupRefresh
					}
				}
	});
	
	
	var userinfo = app.getState() || {};
    var uid = userinfo.user_id;
    var currenPage=1;
    var pageSize=10;
    var total_page=0;
    var total_count=0;
    
    window.onload = function(){
    	mui.ajax({
				url: apiurl + "Store/store_good_list/",
				data: {'p':currenPage,'size':pageSize,'user_id':uid,'_token':userinfo.token},
				async: true,
				dataType: 'json',
				crossDomain: true, //强制使用5+跨域
				type: 'get',
				timeout: 10000,
				success: function(res) {
					console.log(JSON.stringify(res));
					// 请求成功
					if(res.status ==-3){
						mui.toast(res.msg);
						app.out();
						return false;
					} else {
						total_page=res.total_page;
						total_count=res.sum;
						var htm=get_data(res.resource,true);
						$('#pullrefresh .mui-scroll .mui-table-view').append(htm);
					}
				},
				error: function(xhr, type, errorThrown) {
					//console.log(JSON.stringify(xhr));
					//console.log(JSON.stringify(type));
					//console.log(JSON.stringify(errorThrown));
					mui.toast('获取数据失败！');
				}
			});
			
			 mui.plusReady(function(){
			    //关闭等待框
			    plus.nativeUI.closeWaiting();
			    //显示当前页面
			    mui.currentWebview.show();
			    
			    document.getElementById('add_shop').addEventListener('tap', function() {
					  //打开添加商品页
					  mui.openWindow({
					    id:'add-shop.html',
					    url:'add-shop.html',
					    extras:{
					      goods_id:0 //自定义扩展参数，可以用来处理页面间传值
					    },
					    show: {
							aniShow: 'pop-in'
						},
						waiting: {
							autoShow: false
						}
					  });
				}, false);
			
				//预览
				mui(".mui-table-view").on('tap','.mui-table-view-cell .goods-edit .see_good',function(){
					var gid=this.getAttribute('data-id');
					mui.openWindow({
					    id:'preview_good.html',
					    url:'preview_good.html',
					    extras:{
					       goods_id:gid //自定义扩展参数，可以用来处理页面间传值
					    },
					    show: {
							autoShow:false
						},
						waiting: {
							autoShow: true,
							title:'正在加载...'
						}
				  	});
				})
			    
			
			    //编辑
				mui(".mui-table-view").on('tap','.mui-table-view-cell .goods-edit .edit_good',function(){
					var gid=this.getAttribute('data-id');
					//打开商品编辑页
					mui.openWindow({
						    id:'shop-edit.html',
						    url:'shop-edit.html',
						    extras:{
						       goods_id:gid //自定义扩展参数，可以用来处理页面间传值
						    },
						    show: {
								aniShow: 'pop-in'
							},
							waiting: {
								autoShow: false
							}
					  	});
				})
			
				//删除
				mui(".mui-table-view").on('tap','.mui-table-view-cell .goods-edit .del_good',function(){
					var gid=this.getAttribute('data-id');
					var dood_name=this.getAttribute('good-name');
					var btnArray = ['确定', '取消'];
					mui.confirm('确定要删除该商品吗？', '删除 '+dood_name, btnArray, function(e) {
						if (e.index == 0) {
							mui.ajax({
								url: apiurl + "Store/del_good/",
								data: {'goods_id':gid,'_token':userinfo.token},
								async: true,
								dataType: 'json',
								crossDomain: true, //强制使用5+跨域
								type: 'get',
								timeout: 10000,
								success: function(res) {
									console.log(JSON.stringify(res));
									if(res.status ==-3){
										mui.toast(res.msg);
										app.out();
										return false;
									}
									// 请求成功
									if(res.status==0) {
										mui.toast(res.msg);
									} else {
										mui.toast(res.msg);
										location.href=location;
									}
								},
								error: function(xhr, type, errorThrown) {
									//console.log(JSON.stringify(xhr));
									//console.log(JSON.stringify(type));
									//console.log(JSON.stringify(errorThrown));
									mui.toast('获取数据失败！');
								}
							});
						} else {
							//
						}
					})
				})
				
				
				//上架  下架
				mui(".mui-table-view").on('tap','.mui-table-view-cell .goods-edit .sold_out',function(){
					var gid=this.getAttribute('data-id');
					var dood_name=this.getAttribute('good-name');
					var statu=this.getAttribute('data-statu');
					if(statu==0){
						var str="确定要下架商品'"+dood_name+"'吗？";
						var sname="下架";
					}
					if(statu==1){
						var str="确定要上架商品'"+dood_name+"'吗？";
						var sname="上架";
					}
					if(statu==2){
						var str="广告位为付费推广，具体费用以公告信息为准。";
						var sname="广告位推广【"+dood_name+"】";
					}
					if(statu==3){
						var str="推荐专区为付费推广，具体费用以公告信息为准。";
						var sname="推荐专区推广【"+dood_name+"】";
					}
					if(statu==4){
						var str="促销专区为付费推广，具体费用以公告信息为准。";
						var sname="促销专区推广【"+dood_name+"】";
					}
					var btnArray = ['确定', '取消'];
					mui.confirm(str, sname, btnArray, function(e) {
						if (e.index == 0) {
							mui.ajax({
								url: apiurl + "Store/online_good/",
								data: {'goods_id':gid,'statu':statu,'_token':userinfo.token},
								async: true,
								dataType: 'json',
								crossDomain: true, //强制使用5+跨域
								type: 'get',
								timeout: 10000,
								success: function(res) {
									console.log(JSON.stringify(res));
									// 请求成功
									if(res.status ==-3){
										mui.toast(res.msg);
										app.out();
										return false;
									}else {
										mui.toast(res.msg);
										location.href=location;
									}
								},
								error: function(xhr, type, errorThrown) {
									//console.log(JSON.stringify(xhr));
									//console.log(JSON.stringify(type));
									//console.log(JSON.stringify(errorThrown));
									mui.toast('获取数据失败！');
								}
							});
						} else {
							//
						}
					})
				})
				
				
			    window.addEventListener('refresh_data',function(event){
				   location.href=location;
				});
			
			});

    }

    function get_data(data,rank){
    	var htm='';
    	var j=0;
		for(i in data) {
			/*
			 * <li class="mui-table-view-cell mui-media">
						<a href="javascript:;" style="margin-bottom:0px;">
							<img class="mui-media-object mui-pull-left" src="../../../img/logo.png">
							<div class="mui-media-body">
								苹果iPhone7全网通4G手机中国红
								<p class="mui-ellipsis">价格：3278</p>
								<p class="mui-ellipsis">库存：3</p>
							</div>
							<div class="trangle"><p class="goods-num">01</p></div>
						</a>
						<div class="goods-edit">
							<div class="mui-input-row mui-checkbox mui-left" style="width:40%;float: left;">
								<label>置顶</label>
								<input name="checkbox" value="Item 1" type="checkbox" >
							</div>
							<div class="mui-pull-right edit-r">
								<a href="">预览</a>
								<a href="shop-edit.html">编辑</a>
								<a href="">删除</a>
							</div>
						</div>
					</li>
			 */
			htm += '<li class="mui-table-view-cell mui-media">';
			htm+='<a style="margin-bottom:0px;">';
			if(data[i]['original_img'] && data[i]['original_img'].indexOf('Uploads') !== -1){
				//var imgpath=serverurl + data[i]['original_img'];
				if(data[i]['original_img'].indexOf(upurl+'Uploads') !== -1){
					var imgpath=data[i]['original_img'];
				}
				else{
					var imgpath=upurl+data[i]['original_img'];
				}
			}
			else{
				var imgpath='../../../img/logo.png';
			}
			htm+='<img class="mui-media-object mui-pull-left" src="'+imgpath+'">';
			htm+='<div class="mui-media-body">';
			htm+=data[i]['goods_name'];
			htm+='<p class="mui-ellipsis"><font lang>价格</font>：'+data[i]['shop_price']+'</p>';
			htm+='<p class="mui-ellipsis"><font lang>库存</font>：'+data[i]['store_count']+'</p>';
			htm+='</div>';
			if(rank==true){
				j++;
				if(j<10){
					htm+='<div class="trangle"><p class="goods-num">0'+j+'</p></div>';
				}
				else if(j==10){
					htm+='<div class="trangle"><p class="goods-num">'+j+'</p></div>';
				}
			}
			htm+='</a>';
			
			htm+='<div class="goods-edit">';
			if(data[i]['is_on_sale']==1){
/*				htm+='<div class="mui-input-row mui-checkbox mui-left" style="width:40%;float: left;">';
				htm+='<label>置顶</label>';
				if(data[i]['is_recommend']==1){
					htm+='<input name="checkbox" value="1" type="checkbox" checked onclick="set_good(\''+data[i]['goods_id']+'\',this)">';
				}
				else{
					htm+='<input name="checkbox" value="1" type="checkbox" onclick="set_good(\''+data[i]['goods_id']+'\',this)">';
				}
				htm+='</div>';*/
			}
			else{
				htm+='<div class="mui-input-row mui-checkbox mui-left" style="width:40%;float: left;">';
				htm+='<label style="color:#f00;padding-left:0px;">商品已下架</label>';
				htm+='</div>';
			}
			
			htm+='<div class="mui-pull-right edit-r">';
			if(data[i]['is_on_sale']==1){
				htm+='<a class="sold_out" style="color:#f00;" data-id="'+data[i]['goods_id']+'" good-name="'+data[i]['goods_name']+'" data-statu="0">下架</a>';

				htm+='<a class="sold_out" style="color:#f00;" data-id="'+data[i]['goods_id']+'" good-name="'+data[i]['goods_name']+'" data-statu="2">广告位</a>';
				htm+='<a class="sold_out" style="color:#f00;" data-id="'+data[i]['goods_id']+'" good-name="'+data[i]['goods_name']+'" data-statu="3">推荐专区</a>';
				htm+='<a class="sold_out" style="color:#f00;" data-id="'+data[i]['goods_id']+'" good-name="'+data[i]['goods_name']+'" data-statu="4">促销专区</a>';
			}
			if(data[i]['is_on_sale']==0){
				htm+='<a class="sold_out" style="color:#5a98de;" data-id="'+data[i]['goods_id']+'" good-name="'+data[i]['goods_name']+'" data-statu="1">上架</a>';
			}
			htm+='<a class="see_good" data-id="'+data[i]['goods_id']+'">预览</a>';
			htm+='<a class="edit_good"  data-id="'+data[i]['goods_id']+'">编辑</a>';
			htm+='<a class="del_good"  data-id="'+data[i]['goods_id']+'" good-name="'+data[i]['goods_name']+'">删除</a>';
			htm+='</div>';
			htm+='</div>';
			htm+='</li>';
		}
		return htm;
    }
	function pullupRefresh() {
		setTimeout(function() {
			currenPage++;
			mui.ajax({
				url: apiurl + "Store/store_good_list/",
				data: {'p':currenPage,'size':pageSize,'user_id':uid,'_token':userinfo.token},
				async: true,
				dataType: 'json',
				crossDomain: true, //强制使用5+跨域
				type: 'get',
				timeout: 10000,
				success: function(res) {
					console.log(JSON.stringify(res));
					// 请求成功
					if(res.status ==-3){
						mui.toast(res.msg);
						app.out();
						return false;
					} else {
						var htm=get_data(res.resource,false);
						$('#pullrefresh .mui-scroll .mui-table-view').append(htm);
					}
					
					if(currenPage < total_page){
	                    mui('#pullrefresh').pullRefresh().endPullupToRefresh(false);    /*能上拉*/
	                }else{
	                    mui('#pullrefresh').pullRefresh().endPullupToRefresh(true);/*不能上拉*/
	                }
				},
				error: function(xhr, type, errorThrown) {
					//console.log(JSON.stringify(xhr));
					//console.log(JSON.stringify(type));
					//console.log(JSON.stringify(errorThrown));
					mui.toast('获取数据失败！');
				}
			});
			
		}, 1500);
	}
	

    //置顶
	function set_good(goods_id,obj){
		var Info={
			'goods_id':goods_id,
			'_token':userinfo.token
		};
		if(obj.checked){
			Info.is_recommend=1;
		}
		else{
			Info.is_recommend=0;
		}
		mui.ajax({
			url: apiurl + "Store/stick_good/",
			data: Info,
			async: true,
			dataType: 'json',
			crossDomain: true, //强制使用5+跨域
			type: 'get',
			timeout: 10000,
			success: function(res) {
				console.log(JSON.stringify(res));
				// 请求成功
				if(res.status ==-3){
					mui.toast(res.msg);
					app.out();
					return false;
				} else {
					mui.toast(res.msg);
					location.href = location;
				}
			},
			error: function(xhr, type, errorThrown) {
				//console.log(JSON.stringify(xhr));
				//console.log(JSON.stringify(type));
				//console.log(JSON.stringify(errorThrown));
				mui.toast('获取数据失败！');
			}
		});
	}
